const numberUtil = require('./numberUtil')
const averageUtil = require('./averageUtil')
const stockAnalysisUtil = require('./stockAnalysisUtil')

const flagUtil = {
  // 计算指数的平均波动
  countRateAver (recentNetValue) {
    const xData = []
    // 0到7的波动
    for (let j = 0; j < 7; j = j + 0.1) {
      xData.push({
        number: j.toFixed(1),
        count: 0,
        countList: [],
        count2: 0,
        countList2: []
      })
    }
    let sum = 0
    // 净值波动
    const list = []
    // 振幅波动
    const wList = []
    recentNetValue.forEach((item) => {
      const value = Math.abs(item.netChangeRatio || 0)
      sum += value
      // 净值波动
      list.push(value)
      // 振幅波动
      const wave = numberUtil.countDifferenceRate(item.high, item.low)
      wList.push(Math.abs(wave))
      // 分布
      for (let i = 0; i < xData.length; i++) {
        if (value >= xData[i].number && xData[i + 1] && value < xData[i + 1].number) {
          xData[i].count++
          xData[i].countList.push(value)
          break
        }
      }
    })
    // 排序
    xData.sort((a, b) => {
      return b.count - a.count
    })
    list.sort((a, b) => {
      return a - b
    })
    wList.sort((a, b) => {
      return a - b
    })
    // 去除最低部分和最高部分，他们没有统计意义
    const list2 = list.slice(
      parseInt(list.length * 0.17),
      parseInt(list.length * 0.83)
    )
    const wList2 = wList.slice(
      parseInt(wList.length * 0.17),
      parseInt(wList.length * 0.83)
    )
    // 剩余部分的和
    let sum4 = 0
    list2.forEach((item, i) => {
      sum4 += item
    })
    let sum5 = 0
    wList2.forEach((item, i) => {
      sum5 += item
    })
    console.log(xData)
    console.log(sum / recentNetValue.length)
    console.log(list2.length)
    console.log(sum4 / list2.length)
    console.log(wList2.length)
    console.log((sum5 / 2) / wList2.length)
    console.log(`rate:${sum4 / list2.length},wave:${(sum5 / 2) / wList2.length},`)
  },
  // 获取均线信息
  getAverageListInfo (recentNetValue) {
    // 日期近->远
    const average5List = averageUtil.getAverageList(recentNetValue, 5)
    const average10List = averageUtil.getAverageList(recentNetValue, 10)
    const monthList = averageUtil.getAverageList(recentNetValue, 20)
    const average25List = averageUtil.getAverageList(recentNetValue, 25)
    const average30List = averageUtil.getAverageList(recentNetValue, 30)
    const average40List = averageUtil.getAverageList(recentNetValue, 40)
    const average50List = averageUtil.getAverageList(recentNetValue, 50)
    const quarterList = averageUtil.getAverageList(recentNetValue, 60)
    const halfYearList = averageUtil.getAverageList(recentNetValue, 120)
    const yearList = averageUtil.getAverageList(recentNetValue, 250)
    return {
      average5List,
      average10List,
      monthList,
      average25List,
      average30List,
      average40List,
      average50List,
      quarterList,
      halfYearList,
      yearList
    }
  },
  // 获取当天信号
  getDayFlagInfo (yData, recentNetValue) {
    const averageListInfo = this.getAverageListInfo(recentNetValue)
    const diffList = []
    const trendList = flagUtil.countTrendList(yData, averageListInfo.quarterList)
    yData.forEach((item, index) => {
      const diffRate5 = numberUtil.countDifferenceRate(item, averageListInfo.average5List[index])
      const diffRate10 = numberUtil.countDifferenceRate(item, averageListInfo.average10List[index])
      const diffRateMonth = numberUtil.countDifferenceRate(item, averageListInfo.monthList[index])
      const diffRate25 = numberUtil.countDifferenceRate(item, averageListInfo.average25List[index])
      const diffRate30 = numberUtil.countDifferenceRate(item, averageListInfo.average30List[index])
      const diffRate40 = numberUtil.countDifferenceRate(item, averageListInfo.average40List[index])
      const diffRate50 = numberUtil.countDifferenceRate(item, averageListInfo.average50List[index])
      const diffRateQuarter = numberUtil.countDifferenceRate(item, averageListInfo.quarterList[index])
      const diffRateYear = numberUtil.countDifferenceRate(item, averageListInfo.yearList[index])
      const diffRateHalfYear = numberUtil.countDifferenceRate(item, averageListInfo.halfYearList[index])
      const trendRate = trendList[index]
      const noSell = flagUtil.isNoSell(index, yData, averageListInfo.monthList)
      const isQuarterYearDanger = flagUtil.isQuarterYearDanger(averageListInfo.quarterList, averageListInfo.yearList, index, diffRateYear)
      diffList.push({
        diffRate5,
        diffRateMonth,
        diffRateQuarter,
        diffRateYear,
        diffRateHalfYear,
        noSell,
        trendRate,
        diffRate25,
        diffRate30,
        diffRate10,
        diffRate40,
        diffRate50,
        monthClose: averageListInfo.monthList[index],
        quarterClose: averageListInfo.quarterList[index],
        yearClose: averageListInfo.yearList[index],
        halfYearClose: averageListInfo.halfYearList[index],
        isQuarterYearDanger
      })
    })
    return diffList
  },
  // 获取连续性信号，比如是否处于危险中
  getContinuityFlag (dayFlagList, indexItem) {
    let qH = false
    const qHList = []
    let isFix = false
    const isFixList = []
    // const isOneDeep = false
    const isOneDeepList = []
    let noSellFlag = false
    const noSellLine = []
    let noSellChangeIndex = 0
    let monthUpDay = 1
    let monthDownDay = 1
    dayFlagList.forEach((diffInfo, index) => {
      if (diffInfo.diffRateQuarter < 0) {
        qH = false
      }
      if (diffInfo.diffRateQuarter > indexItem.quarterHotLine) {
        qH = true
      }
      qHList.push(qH)
      if (diffInfo.diffRateHalfYear > indexItem.relieveFixLine) {
        isFix = false
      }
      if (diffInfo.diffRateHalfYear < indexItem.fixLine) {
        isFix = true
      }
      isFixList.push(isFix)
      if (diffInfo.diffRateMonth > 0) {
        diffInfo.monthUpDay = monthUpDay
        monthUpDay++
        monthDownDay = 1
      } else {
        diffInfo.monthDownDay = monthDownDay
        monthDownDay++
        monthUpDay = 1
      }
      if (diffInfo.noSell) {
        if (noSellFlag === false) {
          noSellFlag = true
          noSellLine.push({
            gte: noSellChangeIndex,
            lte: index,
            color: 'rgb(160,160,160)'
          })
          noSellChangeIndex = index
        }
      } else {
        if (noSellFlag === true) {
          noSellFlag = false
          noSellLine.push({
            gte: noSellChangeIndex,
            lt: index,
            color: 'rgba(255,0,0, 0.6)'
          })
          noSellChangeIndex = index
        }
      }
    })
    return {
      qHList,
      isFixList,
      isOneDeepList,
      noSellLine
    }
  },
  // 计算季度趋势线
  countTrendList (closeList, quarterList, diff) {
    diff = diff || 30
    // 日期远->近
    const diffRateQuarterList = []
    closeList.forEach((item, index) => {
      const diffRateQuarter = numberUtil.countDifferenceRate(item, quarterList[index])
      // 季度的rate也是一条曲线
      diffRateQuarterList.push(diffRateQuarter + diff)
    })
    // 把季度rate线做了一次20日均线
    const diffRateQuarterAverage20List = averageUtil.getAverageDiffList(diffRateQuarterList, 20)
    const trendRateList = []
    closeList.forEach((item, index) => {
      // 计算季度rate和季度rate的20日线的差值
      const trendRate = numberUtil.countDifferenceRate(diffRateQuarterList[index], diffRateQuarterAverage20List[index])
      trendRateList.push(trendRate)
    })
    return trendRateList
  },
  isNoSell (index, closeList, monthList) {
    const day = 5
    const averageList = []
    if (index >= day) {
      for (let i = day; i >= 0; i--) {
        const nowIndex = index - i
        averageList.push(numberUtil.countDifferenceRate(closeList[nowIndex], monthList[nowIndex]))
      }
    }
    let noSell = stockAnalysisUtil.ifNoSell(averageList)
    // 移动均线策略
    let diff = 0
    if (index > 10) {
      let now = 0
      let last = 0
      let c = true
      // 近的在前
      for (let i = 0; i < 7; i++) {
        if (closeList[index - i]) {
          now += parseFloat(closeList[index - i])
        }
      }
      for (let j = 1; j < 8; j++) {
        if (closeList[index - j]) {
          last += parseFloat(closeList[index - j])
        }
      }
      diff = numberUtil.countDifferenceRate(now / 7, last / 7)
      if (diff < 0.2) {
        c = false
      }
      if (!c) {
        noSell = false
      }
    }
    return noSell
  },
  // 是否是季度年危险
  isQuarterYearDanger (quarterList, yearList, index, diffRateYear) {
    const quarterYearDiff = numberUtil.countDifferenceRate(quarterList[index], yearList[index])
    let isQuarterYearDanger = false
    if (diffRateYear > 0) {
      isQuarterYearDanger = quarterYearDiff < 0
    } else {
      isQuarterYearDanger = quarterYearDiff < 2
    }
    return isQuarterYearDanger
  }
}

module.exports = flagUtil
